from django.shortcuts import render,HttpResponse,redirect
from app01 import models
# Create your views here.


def book_list(request):

	books=models.Book.objects.all()

	return render(request, 'book_list.html', {'books': books})


def add_book(request):
	if request.method == 'GET':

		return render(request,'add_book.html')
	else:
		print(request.POST)
		title=request.POST.get('title')
		price=request.POST.get('price')
		pub_date=request.POST.get('pub_date')
		publish=request.POST.get('publish')

		ret = models.Book.objects.create(
			title=title,
			price=price,
			pub_date=pub_date,
			publish=publish,
		)
		return redirect('/book_list/')

def edit_book(request,id):
	obj_list = models.Book.objects.filter(pk=id)
	if request.method == "GET":
		obj = obj_list.first()

		return render(request,'edit_book.html',{'obj':obj})

	else:
		print(request.POST)
		obj_list.update(
			**request.POST.dict()
		)
		return redirect('book_list')

def delete_book(request,id):
	models.Book.objects.filter(pk=id).delete()
	return redirect('book_list')



